/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package transaction.add_committee;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import template.RegistrantionPanel;
import transaction.add_committee.object.Committee;
import transaction.add_committee.object.Member;
import transaction.add_committee.object.Year;
import transaction.add_committee.object.YearCommittee;
import zsystem.SystemMessage;

/**
 *
 * @author User
 */
public class PCAddCommittee extends RegistrantionPanel {

    /**
     * Creates new form PCAddCommittee
     */
    public PCAddCommittee() {
        initComponents();
        initOthers();
    }

    public void resetAll(){
        initOthers();
    }
    private Object[] getYears() {
        List list = SERAddCommittee.getYears();
        Object[] objects = list.toArray(new Object[list.size()]);
        return objects;
    }

    private Object[] getCommittees() {
        List list = SERAddCommittee.getCommittees();
        Object[] objects = list.toArray(new Object[list.size()]);
        return objects;
    }

    private Object[] getMembers() {
        leftList = SERAddCommittee.getMembers();
        memberList.addAll(leftList);
        Object[] objects = leftList.toArray(new Object[leftList.size()]);
        return objects;
    }

    private void getCommitteeMembers() {

        if (cboPost.getSelectedItem() != null && cboYear.getSelectedItem() != null) {

            List list = SERAddCommittee.getCommitteeMembers(
                    ((Committee) cboPost.getSelectedItem()).getCode(),
                    ((Year) cboYear.getSelectedItem()).getCode());
            rightList.clear();
            leftList.clear();
            leftList.addAll(memberList);
            for (Iterator it = list.iterator(); it.hasNext();) {
                YearCommittee object = (YearCommittee) it.next();
                object.getMember().setSave(true);
                rightList.add(object.getMember());
                for (Object leftList1 : leftList) {
                    if (((Member) leftList1).getCode().equals((object.getMember()).getCode())) {
                        leftList.remove((Member) leftList1);
                        break;
                    }

                }
            }
        }
    }

    private void loadLeftTable() {
        resetTable(tblLeftTable);
        List list = leftList;
        DefaultTableModel tableModel = (DefaultTableModel) tblLeftTable.getModel();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Member member = (Member) it.next();
            tableModel.addRow(new Object[]{member, member.getName()});
        }
    }

    private void loadRightTable() {
        resetTable(tblRightTable);
        List list = rightList;
        DefaultTableModel tableModel = (DefaultTableModel) tblRightTable.getModel();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Member member = (Member) it.next();
            tableModel.addRow(new Object[]{member, member.getName()});
        }
    }

    private void resetTable(JTable table) {
        int rowCount = table.getRowCount();
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        for (int i = 0; i < rowCount; i++) {
            tableModel.removeRow(0);
        }
    }

    private void addRemove(
            JTable fromTable,
            List fromList,
            JTable toTable,
            List toList) {
        int selectedRowCount = fromTable.getSelectedRowCount();
        DefaultTableModel tableModel = (DefaultTableModel) fromTable.getModel();
        if (selectedRowCount > 0) {
            if (cboPost.getSelectedItem() != null || cboYear.getSelectedItem() != null) {
                int selectedRow = fromTable.getSelectedRow();
                Member member = (Member) tableModel.getValueAt(selectedRow, 0);
                if (!member.isSaved()) {
                    toList.add(member);
                    fromList.remove(member);
                    loadLeftTable();
                    loadRightTable();
                    setAdded();
                }
            } else {
                SystemMessage.warningMessage("Warning", "Pleas select the year and committee...");
            }
        }
    }

    private void cboPostActionPerformed(ActionEvent e) {
        if (cboPost.getItemCount() > 0 && cboPost.getSelectedItem() != null) {
            txtNoOfMembers.setText(
                    ((Committee) cboPost.getSelectedItem())
                    .getNumberOfMembers().toString());
            getCommitteeMembers();
            loadRightTable();
            loadLeftTable();

            setAdded();
        }
    }

    private void cboYearActionPerformed(ActionEvent e) {
        if (cboYear.getItemCount() > 0 && cboYear.getSelectedItem() != null) {
            getCommitteeMembers();
            loadRightTable();
            loadLeftTable();

            setAdded();
        }
    }

    private void setAdded() {
        if (cboPost.getSelectedItem() != null) {
            int noOfMembers = ((Committee) cboPost.getSelectedItem())
                    .getNumberOfMembers();
            int noOfAdded = rightList.size();
            lblAdded.setText(noOfAdded + " / " + noOfMembers);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initOthers() {
        lblAdded.setText("0 / 0");
        rightList = new ArrayList();
        leftList = new ArrayList();
        memberList = new ArrayList();
        cboPost.addActionListener(
                new java.awt.event.ActionListener() {
                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        cboPostActionPerformed(evt);
                    }
                }
        );

        cboYear.addActionListener(
                new java.awt.event.ActionListener() {
                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        cboYearActionPerformed(evt);
                    }
                }
        );
        getMembers();
        loadLeftTable();
        loadRightTable();
        cboPost.setSelectedItem(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cboPost = new javax.swing.JComboBox(getCommittees());
        jLabel2 = new javax.swing.JLabel();
        txtNoOfMembers = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblLeftTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblRightTable = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        txtSearch = new javax.swing.JTextField();
        lblAdded = new javax.swing.JLabel();
        cboYear = new javax.swing.JComboBox(getYears());
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(588, 468));

        jLabel1.setText("Post :");

        jLabel2.setText("No of Members :");

        txtNoOfMembers.setEditable(false);

        tblLeftTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Code", "Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblLeftTable);
        if (tblLeftTable.getColumnModel().getColumnCount() > 0) {
            tblLeftTable.getColumnModel().getColumn(0).setMinWidth(20);
            tblLeftTable.getColumnModel().getColumn(0).setPreferredWidth(50);
            tblLeftTable.getColumnModel().getColumn(0).setMaxWidth(150);
            tblLeftTable.getColumnModel().getColumn(1).setMinWidth(60);
            tblLeftTable.getColumnModel().getColumn(1).setPreferredWidth(150);
            tblLeftTable.getColumnModel().getColumn(1).setMaxWidth(450);
        }

        tblRightTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Code", "Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblRightTable);
        if (tblRightTable.getColumnModel().getColumnCount() > 0) {
            tblRightTable.getColumnModel().getColumn(0).setMinWidth(20);
            tblRightTable.getColumnModel().getColumn(0).setPreferredWidth(50);
            tblRightTable.getColumnModel().getColumn(0).setMaxWidth(150);
            tblRightTable.getColumnModel().getColumn(1).setMinWidth(60);
            tblRightTable.getColumnModel().getColumn(1).setPreferredWidth(150);
            tblRightTable.getColumnModel().getColumn(1).setMaxWidth(450);
        }

        btnAdd.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnAdd.setText(">>");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnRemove.setText("<<");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        lblAdded.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblAdded.setText("0/1");

        jLabel8.setText("Year :");

        jLabel9.setText("Search :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtSearch)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAdd)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnRemove)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblAdded))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboPost, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNoOfMembers, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboYear, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboPost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNoOfMembers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblAdded, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAdd)
                            .addComponent(btnRemove)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 11, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        int noOfMembers = ((Committee) cboPost
                .getSelectedItem())
                .getNumberOfMembers();
        if (rightList.isEmpty() || rightList.size() < noOfMembers) {
            addRemove(tblLeftTable, leftList, tblRightTable, rightList);

        } else {
            SystemMessage.warningMessage("Pool overflow.", "Max no of members in this committee is " + ((Committee) cboPost.getSelectedItem()).getNumberOfMembers());
        }

    }//GEN-LAST:event_btnAddActionPerformed

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        addRemove(tblRightTable, rightList, tblLeftTable, leftList);
    }//GEN-LAST:event_btnRemoveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnRemove;
    private javax.swing.JComboBox cboPost;
    private javax.swing.JComboBox cboYear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAdded;
    private javax.swing.JTable tblLeftTable;
    private javax.swing.JTable tblRightTable;
    private javax.swing.JTextField txtNoOfMembers;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
    private List leftList;
    private List rightList;
    private List memberList;

    @Override
    public List<Component> getIdentityComponent() {
        return Arrays.asList(
                (Component) cboPost,
                (Component) cboYear,
                (Component) txtNoOfMembers,
                (Component) tblRightTable
        );
    }

    @Override
    public List<Component> getEsentialComponent() {
        return Arrays.asList();
    }

    @Override
    public List<Component> getOtherComponent() {
        return Arrays.asList(
                (Component) txtNoOfMembers
        );
    }

    @Override
    public Object initObject() {
        return new ArrayList<>();
    }

    @Override
    public Object[] initObjectList() {
        int row = tblRightTable.getRowCount();
        if (row != 0) {
            Object[] yearCommittees = new Object[]{};
            ArrayList arrayList = new ArrayList();

            for (int i = 0; i < row; i++) {
                YearCommittee yearCommittee = new YearCommittee();
                Member member = new Member();
                yearCommittee.setYear((Year) cboYear.getSelectedItem());
                yearCommittee.setCommittee((Committee) cboPost.getSelectedItem());
                yearCommittee.setNoOfMembers(Integer.parseInt(txtNoOfMembers.getText()));
                member = ((Member) tblRightTable.getValueAt(i, 0));
                member.setCommittee(((Committee) cboPost.getSelectedItem()).getCode());
                yearCommittee.setMember(member);
                yearCommittee.setStatus(YearCommitteeStatus.ACTIVE);
                arrayList.add(yearCommittee);
            }
            yearCommittees = arrayList.toArray();
            return yearCommittees;
        } else {
            return null;
        }
    }

    @Override
    public void getUnbindObject(Object object) {
    }

    @Override
    public void bindComponents(ArrayList<Object> list, int index) {
    }

    @Override
    public List<Component> componentList() {
        return Arrays.asList(
                (Component) cboYear,
                (Component) cboPost,
                (Component) txtNoOfMembers,
                (Component) tblRightTable
        );
    }

}
